package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.CheckGroupDao;
import com.itheima.health.dao.CheckgroupCheckitemDao;
import com.itheima.health.dao.SetmealCheckGroupDao;
import com.itheima.health.pojo.dto.QueryPageBeanDTO;
import com.itheima.health.pojo.entity.CheckGroup;
import com.itheima.health.pojo.result.PageResult;
import com.itheima.health.pojo.result.Result;
import com.itheima.health.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 新增检查组
 */
@Service
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;
    @Autowired
    private CheckgroupCheckitemDao checkgroupCheckitemDao;
    @Autowired
    private SetmealCheckGroupDao setmealCheckGroupDao;

    /**
     * 新增检查组
     *
     * @param checkGroup
     * @param checkitemIds
     */
    @Transactional
    @Override
    public void add(CheckGroup checkGroup, List<Integer> checkitemIds) {
        checkGroupDao.add(checkGroup);
        Integer checkGroupId = checkGroup.getId();
        if (!checkitemIds.isEmpty()) {
            for (Integer itemId : checkitemIds) {
                checkgroupCheckitemDao.addCheckGroupCheckItem(checkGroupId, itemId);
            }
        }
    }

    @Override
    public PageResult pageQuery(QueryPageBeanDTO queryPageBeanDTO) {
        PageHelper.startPage(queryPageBeanDTO.getCurrentPage(), queryPageBeanDTO.getPageSize());
        Page<CheckGroup> page = checkGroupDao.selectByConditon(queryPageBeanDTO.getQueryString());
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public CheckGroup findById(Integer id) {
        CheckGroup checkGroup = checkGroupDao.findCheckGroupById(id);
        return checkGroup;
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer checkGroupId) {
        List<Integer> checkItems = checkgroupCheckitemDao.findCheckItemIdsByCheckGroupId(checkGroupId);
        return checkItems;
    }

    @Transactional
    @Override
    public void edit(CheckGroup checkGroup, List<Integer> checkitemIds) {
        //1.修改检查组信息
        checkGroupDao.update(checkGroup);
        //2.修改关系表
        //先删除再增加
        checkgroupCheckitemDao.deleteByCheckGroupId(checkGroup.getId());
//        if (!checkitemIds.isEmpty()){
//            for (Integer checkitemId : checkitemIds) {
//                checkgroupCheckitemDao.addCheckGroupCheckItem(checkGroup.getId(),checkitemId);
//            }
//        }
        //封装到map里送到mapper，在数据库里遍历插入提升性能
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("checkGroup", checkGroup);
        paramMap.put("checkitemIds", checkitemIds);
        checkgroupCheckitemDao.batchInsertCheckGroupCheckItem(paramMap);
    }

    /**
     * 根据id删除检查组
     *
     * @param id
     * @return
     */
    @Override
    public void deleteCheckGroupitemById(Integer id) {
        //查询是否有套餐绑定该检查组，有则不能删除
        Integer count = setmealCheckGroupDao.countByGroupId(id);
        if (count > 0) {
            throw new RuntimeException(MessageConst.DELETE_CHECKGROUP_FAIL);
        }
        //删除检查组表和关系表中数据
        checkGroupDao.deletByGroupId(id);
        checkgroupCheckitemDao.deleteByCheckGroupId(id);
    }

    /**
     * 查询全部检查组信息
     *
     * @return
     */
    @Override
    public List<CheckGroup> findALL() {
        List<CheckGroup> checkGroups = checkGroupDao.findAll();
        return checkGroups;
    }


}
